package presentation;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;

public class SelectionBox {

	private int fromX;
	private int fromY;

	private int toX;
	private int toY;

	private int length;

	public SelectionBox(int fromX, int fromY, int toX, int toY) {

		// local declarations
		int diffX;
		int diffY;

		boolean switchedX = false;
		boolean switchedY = false;

		this.fromX = fromX;
		this.fromY = fromY;
		this.toX = toX;
		this.toY = toY;

		diffX = Math.abs(this.fromX - this.toX);
		diffY = Math.abs(this.fromY - this.toY);

		if (diffX < diffY) {
			this.length = diffX;
		} else {
			this.length = diffY;
		}

		if (toX - fromX < 0) {
			this.fromX = toX;
			this.toX = fromX;

			switchedX = true;
		}

		if (toY - fromY < 0) {
			this.fromY = toY;
			this.toY = fromY;

			switchedY = true;
		}

		diffX = Math.abs(this.fromX - this.toX);
		diffY = Math.abs(this.fromY - this.toY);

		if (switchedX) {
			if (diffX > length) {
				this.fromX += (diffX - length);
			}
		}

		if (switchedY) {
			if (diffY > length) {
				this.fromY += (diffY - length);
			}
		}

	}

	/**
	 * Zeichnet die quadratische Auswahlbox für den Zoom.
	 * 
	 * @param graphics
	 *            Der Grafikkontext, mit dem die Auswahlbox gezeichnet wird.
	 */
	public void draw(Graphics2D graphics) {
		graphics.setStroke(new BasicStroke(4f));
		graphics.setPaint(Color.WHITE);
		graphics.drawRect(this.fromX, this.fromY, length, length);
	}

	// getter / setter
	// =============
	
	public int getFromX() {
		return this.fromX;
	}

	public int getFromY() {
		return this.fromY;
	}

	public int getLength() {
		return this.length;
	}
}